#region Using
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using CY.GFive.Core.Business;
using CY.Utility.DBUtility;
#endregion Using

namespace CY.GFive.DALProviders.SqlServerProvider
{
	public class FileItemProvider : CY.GFive.Core.Providers.DALProvider.IFileItemProvider
	{
		#region Sql
		private static readonly string SqlSelectFileItem = "SELECT [Id], [FileItemCode],[ItemCate],[ItemName],[ItemAddress],[FileCode],[FileMaker],[FileStates] FROM [FileItem] WHERE  [Id]=@Id";

		private static readonly string SqlInsertFileItem = "INSERT INTO [FileItem]([FileItemCode],[ItemCate],[ItemName],[ItemAddress],[FileCode],[FileMaker],[FileStates]) values(@FileItemCode,@ItemCate,@ItemName,@ItemAddress,@FileCode,@FileMaker,@FileStates);SELECT SCOPE_IDENTITY();";

		private static readonly string SqlUpdateFileItem = "UPDATE [FileItem] SET [FileItemCode] = @FileItemCode,[ItemCate] = @ItemCate,[ItemName] = @ItemName,[ItemAddress] = @ItemAddress,[FileCode] = @FileCode,[FileMaker] = @FileMaker,[FileStates] = @FileStates WHERE  [Id]=@Id";

		private static readonly string SqlDeleteFileItem = "DELETE [FileItem] WHERE  [Id]=@Id";

		private static readonly string SqlGetAllFileItem = "SELECT [Id], [FileItemCode],[ItemCate],[ItemName],[ItemAddress],[FileCode],[FileMaker],[FileStates] FROM [FileItem]";
		#endregion

		#region IFileItemProvider Members
		public Core.Business.FileItem Select(int id)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@Id", SqlDbType.Int, id);
			SqlDataReader reader = sql.ExecuteSqlReader(SqlSelectFileItem);

			if (reader != null && !reader.IsClosed && reader.Read())
			{
				Core.Business.FileItem fileItem = new Core.Business.FileItem();

				if (!reader.IsDBNull(0)) fileItem.Id = reader.GetInt32(0);
				if (!reader.IsDBNull(1)) fileItem.FileItemCode = reader.GetString(1);
				if (!reader.IsDBNull(2)) fileItem.ItemCate = reader.GetString(2);
				if (!reader.IsDBNull(3)) fileItem.ItemName = reader.GetString(3);
				if (!reader.IsDBNull(4)) fileItem.ItemAddress = reader.GetString(4);
				if (!reader.IsDBNull(5)) fileItem.FileCode = reader.GetString(5);
				if (!reader.IsDBNull(6)) fileItem.FileMaker = reader.GetString(6);
				if (!reader.IsDBNull(7)) fileItem.FileStates = reader.GetString(7);

				return fileItem;
			}
			else
			{
				if (reader != null && !reader.IsClosed)
					reader.Close();

				return null;
			}
		}

		public void Insert(Core.Business.FileItem fileItem)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@FileItemCode", SqlDbType.NVarChar, fileItem.FileItemCode);
			sql.AddParameter("@ItemCate", SqlDbType.NVarChar, fileItem.ItemCate);
			sql.AddParameter("@ItemName", SqlDbType.NVarChar, fileItem.ItemName);
			sql.AddParameter("@ItemAddress", SqlDbType.NVarChar, fileItem.ItemAddress);
			sql.AddParameter("@FileCode", SqlDbType.NVarChar, fileItem.FileCode);
			sql.AddParameter("@FileMaker", SqlDbType.NVarChar, fileItem.FileMaker);
			sql.AddParameter("@FileStates", SqlDbType.NVarChar, fileItem.FileStates);
			SqlDataReader reader = sql.ExecuteSqlReader(SqlInsertFileItem);

			if (reader != null && !reader.IsClosed && reader.Read())
			{
				if (!reader.IsDBNull(0)) fileItem.Id = int.Parse(reader.GetDecimal(0).ToString());

				reader.Close();
			}
			else
			{
				if (reader != null && !reader.IsClosed)
					reader.Close();
			}
		}

		public void Update(Core.Business.FileItem fileItem)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@FileItemCode", SqlDbType.NVarChar, fileItem.FileItemCode);
			sql.AddParameter("@ItemCate", SqlDbType.NVarChar, fileItem.ItemCate);
			sql.AddParameter("@ItemName", SqlDbType.NVarChar, fileItem.ItemName);
			sql.AddParameter("@ItemAddress", SqlDbType.NVarChar, fileItem.ItemAddress);
			sql.AddParameter("@FileCode", SqlDbType.NVarChar, fileItem.FileCode);
			sql.AddParameter("@FileMaker", SqlDbType.NVarChar, fileItem.FileMaker);
			sql.AddParameter("@FileStates", SqlDbType.NVarChar, fileItem.FileStates);
			sql.AddParameter("@Id", SqlDbType.Int, fileItem.Id);
			sql.ExecuteSql(SqlUpdateFileItem);
		}

		public void Delete(Core.Business.FileItem fileItem)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@Id", SqlDbType.Int, fileItem.Id);
			sql.ExecuteSql(SqlDeleteFileItem);
		}

		public IList<Core.Business.FileItem> GetAllFileItem()
		{
			IList<Core.Business.FileItem> fileItemlist = new List<Core.Business.FileItem>();
			SqlServerUtility sql = new SqlServerUtility();

			SqlDataReader reader = sql.ExecuteSqlReader(SqlGetAllFileItem);

			if(reader != null)
			{
				while(reader.Read())
				{
					Core.Business.FileItem fileItem = new Core.Business.FileItem();

					if (!reader.IsDBNull(0)) fileItem.Id = reader.GetInt32(0);
					if (!reader.IsDBNull(1)) fileItem.FileItemCode = reader.GetString(1);
					if (!reader.IsDBNull(2)) fileItem.ItemCate = reader.GetString(2);
					if (!reader.IsDBNull(3)) fileItem.ItemName = reader.GetString(3);
					if (!reader.IsDBNull(4)) fileItem.ItemAddress = reader.GetString(4);
					if (!reader.IsDBNull(5)) fileItem.FileCode = reader.GetString(5);
					if (!reader.IsDBNull(6)) fileItem.FileMaker = reader.GetString(6);
					if (!reader.IsDBNull(7)) fileItem.FileStates = reader.GetString(7);

					fileItem.MarkOld();
					fileItemlist.Add(fileItem);
				}
				reader.Close();
			}
			return fileItemlist;
		}
		#endregion
	}
}
